function loadProduct(){
    $('#productGrid').datagrid({
        url : TP_ADMIN+'/Product/getProductJson',
        pagination : true,
        pagePosition : 'bottom',
        pageSize : 10,
        fit : true,
        fitColumns : true,
        idField : 'product_id',
        columns:[[
        {
            align:'right',
            field:'product_id',
            title:'Product & Service',
            width:60,
            checkbox:true
        },
        {
            align:'left',
            field:'title',
            title:'Name',
            width:100
        },
        {
            align:'left',
            field:'price',
            title:'Price',
            width:100,
            formatter : formatMoney
        },
        {
            align:'left',
            field:'create_at',
            title:'Create At',
            width:160
        },
        {
            align:'left',
            field:'description',
            title:'Description',
            width:240
        }
        ]],
        onDblClickRow : function(rowIndex, row){
            var id = row.product_id;
        }

    });
}
function openProduct(){
    loadProduct();
    $("#productGrid").datagrid('clearSelections');
    var _scrollHeight = $(document).scrollTop();
    $('#productWindow')
    .window('open')
    .window('setTitle','Select Product & Service')
    .window('resize',
    { 
        top:($(window).height() - 480) * 0.5+ _scrollHeight,  
        left:($(window).width() - 800) * 0.5
    }
    );
}
function reloadProduct(){
    $("#productGrid").datagrid('load',{});
    $('#keyword').val('');
}
function searchProduct(){
    var keyword = $('#keyword').val();
    if(keyword) {
        $("#productGrid").datagrid('load',{
            'q' : keyword
        });
    }
}
function countProduct(){
    var itemLength = $('.p_item').length;
    var amount = 0;
    var total = 0;
    var discounts = 0;
    if(itemLength) {
        $('.p_item').each(function(){
            var price = $(this).find('.p_price').find('input').val();
            price = price.replace('$', '').replace(',', '');
            var fdoffset = price.indexOf('.');
            var x = parseInt(price.substring(0,fdoffset))*100;
            var y = parseInt(price.substring(fdoffset+1,fdoffset+3));
            price = x+y;      
            var qty = parseInt($(this).find('.p_qty').find('input').val());
            var discount = parseInt($(this).find('.p_discount').find('input').val());

            if(isNaN(price) || isNaN(qty) || isNaN(discount) ) {
                
                alert('Value can not be empty');
                return false;
            }
            total += price*qty;
            var t = Math.ceil(price*qty*discount/100);
  
            $(this).find('.p_amount').html(formatMoney(t));
            amount += t;
        });
        amount = Math.ceil(amount);
        discounts = total - amount;
        
        $('.product_total').html(formatMoney(total));
        $('.product_amount').html(formatMoney(amount));
        $('.product_discount').html(formatMoney(discounts));
        $('#total').val(total);
        $('#amount').val(amount);
        
    } else {
        alert('Please Select Product & Service First!');
    }
}
function selectProduct(){
    var rows = $("#productGrid").datagrid("getSelections");    // 获取所有选中的行
    var  tempProduct = $( '#product_tmp' ).html();
    var html = '';
    for (var i = 0; rows && i < rows.length; i++) {
        var row = rows[i];
        html += tempProduct
        .replace( /\{title\}/g, row.title)
        .replace( /\{product_id}/g, row.product_id)
        .replace( /\{description\}/g, row.description)
        .replace( /\{price\}/g, formatMoney(row.price));
    }
    $('#productBody').append(html);
    $('#productWindow').window('close');
    countProduct();
}

function deleteProduct(product_id){
    if(!confirm("Are you sure?")){
                return false;  
    } else {       
        $('#p_item_'+product_id).remove();
        var p_length = $('.p_item').length;
        if(p_length) {
            countProduct();
        }else {
            $('.product_total').empty();
            $('.product_amount').empty();
            $('.product_discount').empty();
            $('#total').val('');
            $('#amount').val('');
        }
    }
}

function deleteExitProduct(qp_id){
    $.post(TP_ADMIN+'/Quotation/deleteProduct',{'qp_id' : qp_id}, function(resp){
        if(resp.status == 1){

            deleteProduct(qp_id);
        }
    },'json')
    
}
function deleteInvoiceProduct(ip_id){
    $.post(TP_ADMIN+'/Invoice/deleteProduct',{'ip_id' : ip_id}, function(resp){
        if(resp.status == 1){

            deleteProduct(ip_id);
        }
    },'json')
    
}
$(function(){
        
        $('#productBody input').live('blur',function(){
            countProduct();
        });
    })